//
//  WBHomeViewController.swift
//  itcastWeibo
//
//  Created by 谢臣 on 2019/10/15.
//  Copyright © 2019 itcast. All rights reserved.
//

import UIKit
private let cellId = "cellId"
class WBHomeViewController: WBBaseViewController {
    
   
    ///列表视图模型
    private lazy var listViewModel = WBStatusListViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    /// 加载数据
    override func loadData() {
        
        print("准备刷新，最后一条 \(self.listViewModel.statusList.last?.text)")
        
        listViewModel.loadStatus(pullup: self.isPullup) { (isSuccess, shouldRefresh) in
            
            print("加载数据结束)")
            // 结束刷新控件
            self.refreshControl?.endRefreshing()
            // 恢复上拉刷新标记
            self.isPullup = false
            
            // 刷新表格
            if shouldRefresh {
                self.tableView?.reloadData()
            }
        }
    }
    
    /// 显示好友
    @objc private func showFriends() {
        
        let vc = WBDemoViewController()
        
        // push 的动作是 nav 做的
        navigationController?.pushViewController(vc, animated: true)
    }


    
   
    
  
}

//设置数据源
extension WBHomeViewController{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // 1. 取 cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)

        // 2. 设置 cell
        cell.textLabel?.text = listViewModel.statusList[indexPath.row].text

        // 3. 返回 cell
        return cell
    }
    
}

//设置界面
extension WBHomeViewController{
    override func setupTableView() {
        super.setupTableView()
        //设置导航按钮高亮
        navItem.leftBarButtonItem = UIBarButtonItem(title:"好友", fontSize: 16.0, target: self, action: #selector(showFriends))
        
        //注册圆形 cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    }
    
    
    
}
